<?php

    namespace app\listen\model;

    use think\Exception;

    class UserLove extends Base {
        public function love($uid, $voice_id) {
            try {
                return $this->insert([
                    'uid' => $uid,
                    'voice_id' => $voice_id,
                    'addtime' => request()->time(),
                ]);
            } catch (Exception $e) {
                return false;
            }
        }

        public function dislove($id) {
            try {
                return $this->where([
                    'id' => $id,
                ])->delete();
            } catch (Exception $e) {
                return false;
            }
        }

        public function getLoveCount($voice_id) {
            try {
                return $this->where([
                    'voice_id' => $voice_id,
                ])->count();
            } catch (Exception $e) {
                return 0;
            }
        }

        public function getUseLove($uid, $voice_id) {
            try {
                return $this->where([
                    'uid' => $uid,
                    'voice_id' => $voice_id,
                ])->find();
            } catch (Exception $e) {
                return null;
            }
        }

        public function getLovers($voice_id, $page = 1, $rows = 4) {
            try {
                $field = "v.*,m.nickname,m.head_photo,m.sex";
                return $this->alias("v")
                    ->join('listen_member m', 'v.uid=m.uid', 'LEFT')->field($field)
                    ->where([
                        'v.voice_id' => $voice_id,
                    ])
                    ->order('v.addtime desc')
                    ->page($page, $rows)
                    ->select();
            } catch (Exception $e) {
                return null;
            }
        }
    }